<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta content="telephone=no" name="format-detection" />
<title>微信支付</title>
<!-- 微信支付 -->
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" charset="UTF-8"></script>
<script type="text/javascript">

	//判断浏览器类型
	var ua = navigator.userAgent.toLowerCase();
	var isWeixin = ua.indexOf('micromessenger') != -1;
	var isAndroid = ua.indexOf('android') != -1;
	var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
	if (!isWeixin) {
	    document.head.innerHTML = '<title>抱歉，出错了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css">';
	    document.body.innerHTML = '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">请在微信客户端打开链接</h4></div></div>';
	}
	
	$(function(){
		var submitFlag=false;
		function onBridgeReady(){
			WeixinJSBridge.invoke('getBrandWCPayRequest', {
			           "appId":  $("#appId").val(),     //公众号名称，由商户传入     
			           "timeStamp" : $("#timeStamp").val(),         //时间戳，自1970年以来的秒数     
			           "nonceStr" : $("#nonceStr").val(), //随机串     
			           "package" : $("#package").val(),     
			           "signType" : "MD5",         //微信签名方式：     
			           "paySign" : $("#paySign").val() //微信签名 
			       },
			       function(res){     
			    	 	//使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。 
			           //alert(JSON.stringify(res));
			    	   if(res.err_msg == "get_brand_wcpay_request:ok" ) {
			    		   if(!submitFlag){
			        	   		location.href="${pageContext.request.contextPath}/weixin/pay_ok?orderNo="+$("#order_code").val();
			        	   		submitFlag=true;
			    		   }
			           }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
			        	   if(!submitFlag){
				        	   	location.href="${pageContext.request.contextPath}/weixin/pay_cancel?orderNo="+$("#order_code").val();
				        	   submitFlag=true;
			    		   }
					   }else if(res.err_msg == "get_brand_wcpay_request:fail"){
						   //alert(JSON.stringify(res));
						   if(!submitFlag){
							   location.href="${pageContext.request.contextPath}/weixin/pay_fail?orderNo="+$("#order_code").val();
							   submitFlag=true;
		    		   		}
					   }
			});
		}
		if (typeof WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
						false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
				document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
			}
		} else {
			onBridgeReady();
		} 

	});
</script>

</head>

<body>
	<input type="hidden" id="appId" value="${appId }"/>
	<input type="hidden" id="timeStamp" value="${timeStamp }"/>
	<input type="hidden" id="nonceStr" value="${nonceStr }"/>
	<input type="hidden" id="package" value="${package_s }"/>
	<input type="hidden" id="paySign" value="${paySign }"/>
	<input type="hidden" id="order_code" value="${order_code }"/>
</body>
</html>